﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
using System.Xml;
using System.Xml.Linq;

namespace XMLMovieWebsite
{
    public partial class Register : System.Web.UI.Page
    {
        int aa = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (txtPass.Text.Length > 5 && txtPass.Text.Length < 17)
            {
                Label2.Visible = false;
                if (txtUsername.Text.Length < 6 || txtUsername.Text.Length > 16)
                    Label3.Visible = true;
                else
                {
                    if (checkUser() == 1)
                    {
                        Label3.Visible = true;
                        Label3.Text = "username đã tồn tại";
                    }
                    else
                    {
                        insert();
                        Label3.Visible = false;
                        Label1.Visible = true;
                        Panel1.Visible = false;
                    }
                }
            }
            else
            {
                Label2.Visible = true;
                Label2.Text = "Pass phai tu 6-16 ky tu";
            }


        }

        public void insert()
        {

                string ConString = ConfigurationManager.ConnectionStrings["ConStringXml"].ConnectionString;
                string CMDString = " insert into taikhoan values ('"+txtUsername.Text+"','"+txtPass.Text+"','"
                                   + txtEmail.Text+"','"+txtDiachi.Text+"','"+DateTime.Today.ToShortDateString()+"' )";
                SqlConnection con;
                SqlCommand cmd;
                SqlDataAdapter sda;
                DataTable dt;


                using (con = new SqlConnection(ConString))
                {
                    cmd = new SqlCommand(CMDString, con);
                    con.Open();
                    aa = cmd.ExecuteNonQuery();
                    con.Close();

                }
            }

        public int checkUser()
        {

            string ConString = ConfigurationManager.ConnectionStrings["ConStringXml"].ConnectionString;
            string CMDString = " SELECT  * FROM taikhoan where username='"+txtUsername.Text+"'" ;
            SqlConnection con;
            SqlCommand cmd;
            SqlDataAdapter sda;
            DataTable dt;


            using (con = new SqlConnection(ConString))
            {
                cmd = new SqlCommand(CMDString, con);
                con.Open();
                dt = new DataTable("Movie");
                sda = new SqlDataAdapter(cmd);
                sda.Fill(dt);
                if (dt.Rows.Count > 0)
                    return 1;
                else
                    return 0;
                con.Close();

            }
        }

        }
    
}